﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using sqlserverextensions.Helper;


    public partial class UserDefinedFunctions
    {
        private static Int64 lineNum;


        [Microsoft.SqlServer.Server.SqlFunction(
          DataAccess = DataAccessKind.None,
          FillRowMethodName = "FillFileReadAllLines",
          IsDeterministic = true,
          Name = "FileReadAllLines",
          TableDefinition = @" LineNumber BIGINT NULL,
                            Line NVARCHAR(MAX) NULL"
          )]
        public static IEnumerable FileReadAllLines(SqlString path)
        {
            Int16 errSeverity = 16;
            lineNum = 0;
            try
            {
                return System.IO.File.ReadAllLines(path.Value);
            }

            catch (Exception ex)
            {
                ExceptionHelper.EvaluateException(ex, errSeverity);
                return null;
            }
        }

        private static void FillFileReadAllLines(object obj, out SqlInt64 lineNumber, out SqlString line)
        {
            lineNum++;
            try
            {

                lineNumber = lineNum;
                line = obj.ToString();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }


